package com.redstar.HappyRefresh.manage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redstar.HappyRefresh.authority.Authority;
import com.redstar.HappyRefresh.json.SettingScoreJson;
import com.redstar.HappyRefresh.pojo.Result;
import com.redstar.HappyRefresh.pojo.Setting;
import com.redstar.HappyRefresh.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/setting")
public class SettingManage { // 系统设置管理端

    @Autowired
    private SettingService settingService; // 系统设置
    @Autowired
    private ObjectMapper objectMapper; // JSON转换

    @GetMapping
    @Authority("admin:setting:get")
    public Result get() throws JsonProcessingException { // 查询审核策略
        // 读取系统设置
        Setting setting = settingService.list().get(0);
        // 将审核策略反序列化为实体类对象
        SettingScoreJson settingScoreJson = objectMapper.readValue(setting.getAuditPolicy(), SettingScoreJson.class);
        setting.setSettingScoreJson(settingScoreJson);
        return Result.succeed().data(setting);
    }


    @PutMapping
    @Authority("admin:setting:update")
    public Result update(@RequestBody @Validated Setting setting) { // 修改审核策略
        settingService.updateById(setting);
        return Result.succeed().message("修改成功");
    }
}
